// xc_lshop/api/jtklm/meituan/com/poster.js
// var actbill = require('../../model/poster')
var actbill = require('../../../xc/model/poster')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    op: {
      type: String,
      value: 'meituan'
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    list:{

    },
   
  },
  ready(){
    // var bgimg="http://md.cohooo.net/attachment/images/2/2021/10/PsmAyyuxmzA4YTwmS7XK4y7979XfI9.png";
    // var qrimg="http://p0.meituan.net/poiqrcode/74062c815334f1f98f369fd8e6657d9a100811.jpg";
    //  this.init(bgimg,qrimg)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    init(type,bgimg,qrimg,qr){
      var updatedata={};
      updatedata['posterimg']=""
      updatedata['show']=true;
      this.setData(updatedata)
      if(type && this.data.list[type]){
        this.setData({
          'posterimg':this.data.list[type]
        })
      }else{
        this.loadposter(type,bgimg,qrimg,qr)
      }
    },
    loadposter(type,bgimg,qrimg,qr) {
      
      var para = {op:this.data.op};
      para["qrimg"] = qrimg
      para["bgimg"] = bgimg
      para["type"] = type
      para["qr"] = qr
    
      var that = this;
      actbill.poster(para).then(function (res) {
        

        if(res['status']==1){

          if(type){
            var name='list['+type+"]";
            that.setData({
              [name]:res['url'],
              posterimg:res['url']
            })
          }else{

            that.setData({
              posterimg:res['url']
            })
          }
       
        }
       
        console.log(that.data.list)

      })
    },
    onClose(){
      this.setData({
        show:false
      })
    },
     //保存海报
     saveImageToPhotosAlbum() {
      var that = this;
      wx.downloadFile({
        url: that.data.posterimg,
        success: function (res) {
          var tempFilePath = res.tempFilePath;
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: function (res) {
              wx.saveImageToPhotosAlbum({
                filePath: tempFilePath,
                success: function (res) {
                  wx.showToast({
                    title: '操作成功',
                  })
                },
                fail: function (res) {
                  if (res.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                    wx.openSetting({
                      success(settingdata) {}
                    })
                  }
                }
              })
            }
          })


        }
      })

    }
  } 
})
